// index.ts

import { getUnionId, checkLogin } from '../../services/login'

// 获取应用实例
const app = getApp<IAppOption>()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Component({
  data: {
    order: {
      id: "E423203041",
      from: "南京",
      to: "武汉",
      startTime: "2024-02-01 15:23",
      terminalTime: "2024-02-03 18:59",
      driverInfo: {
        driverName: "王*狗",
        card:"粤M4232423",
      },
      goodsOwner: {
        name:"苏州哪都能送物流公司"
      }
    }
  },
  lifetimes: {
    attached() {
      this.initUnionid()
      this.login()
    }
  },
  methods: {
    // 事件处理函数
    captcherGas() {
      wx.navigateTo({
        url: "/pages/camera/camera",
        success: () => {
          console.log("gas_captcher")
        },
        fail: (err:any) => {
          console.log(err)
        }
      })
    },
    initUnionid() {
      let data = app.globalData
      let unionid = data.unionid
      console.log('当前unionid:' + unionid)
      if (!unionid) {
        // 登录
        getUnionId((id) => {
          wx.showToast({
            title: "unionid:" + id,
            icon: 'success'
          })
          wx.setStorage({
            key: "unionid",
            data: id,
          })
          app.globalData.unionid = id
        })
      } else {
        wx.showToast({
          title: `已经获取unionid:${unionid}`,
          icon: 'success',
          duration: 5000
        })
      }
    },
    login() {
      let isLogin = checkLogin(app.globalData)
      console.log('检查结果:登录了?' + (isLogin?"是的":"没有"))
      if (!isLogin) {
        console.log('跳转到登录')
        wx.navigateTo({
          url: '../login/login'
        })
        return
      }
    }
  },
})
